package com.yazi.mxz.person.controller.shop;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.StoreRestock;
import com.yazi.mxz.common.entity.beans.StoreRestockGoods;
import com.yazi.mxz.common.entity.enums.StoreRestockGoodsType;
import com.yazi.mxz.common.service.StoreRestockService;
import com.yazi.mxz.person.controller.MyPageLink;

@Controller("shop-storeRestockController")
@RequestMapping("/shop/storeRestock")
public class StoreRestockController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreRestockController.class);
	
	@Resource
	StoreRestockService storeRestockService;
	
	@RequestMapping("/list")
	public String list(StoreRestock storeRestock, Searching si, Model model) {
		debug(log, "进入店铺进货管理列表页面");
		
		Sort sort;
		// “未入库”按添加时间倒序，“已入库”按入库时间倒序
		if (!storeRestock.getIsChecked()) {
			sort = new Sort("addTime", true);
		} else {
			sort = new Sort("checkTime", true);
		}
		List<Sort> sorts = new ArrayList<Sort>();
		sorts.add(sort);
		si.setSorts(sorts);
		si.setPageSize(10);
		
		// 只查询当前店铺
		storeRestock.setStore(getStore()); 
		
		Page<StoreRestock> page = storeRestockService.list(storeRestock, si);
		
		model.addAttribute("list", page.getList());
		model.addAttribute("pl", new MyPageLink(page.getPaging()));
		model.addAttribute("isChecked", storeRestock.getIsChecked());
		return "shop/storeRestock_list";
	}
	
	@RequestMapping("/addOrEdit")
	public String addOrEdit(Long restockId, Model model) {
		debug(log, "进入进货单新增/编辑页面");
		StoreRestock sr = null;
		if (null != restockId) {
			sr = storeRestockService.one(restockId);
			if (null == sr || !sr.getStore().equals(getStore(true))) {
				return REDIRECT_INDEX;
			}
		}
		StoreRestockGoodsType[] srgTypes = StoreRestockGoodsType.values();
		
		model.addAttribute("sr", sr);
		model.addAttribute("srgTypes", srgTypes);
		return "shop/storeRestock_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public StatusResult addOrEditDoJson(StoreRestock storeRestock, Srgses srgses) {
		debug(log, "保存进货单信息");
		StatusResult jsonResult = new StatusResult();
		try {
			storeRestockService.addOrEditBystore(getStore(true), getMember().getName(), storeRestock, srgses.getSrgses());
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "保存店铺进货单异常");
			jsonResult.setErrorMessage("保存失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/restockDo/json")
	@ResponseBody
	public StatusResult restockDoJson(Long restockId) {
		debug(log, "进货单商品入库");
		StatusResult jsonResult = new StatusResult();
		try {
			storeRestockService.editToCheckedByStore(getStore(true), restockId, getMember().getName());
			
			jsonResult.setOk(); 
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "进货单商品入库异常");
			jsonResult.setErrorMessage("入库失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult deleteDoJson(long restockId) {
		debug(log, "删除进货单 ");
		StatusResult jsonResult = new StatusResult();
		try {
			storeRestockService.removeByStore(getStore(true), restockId);
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "删除进货单异常");
			jsonResult.setErrorMessage("删除失败，请联系管理员");
		}
		return jsonResult;
	}
	
	public static class Srgses {
		private List<StoreRestockGoods> srgses;

		public List<StoreRestockGoods> getSrgses() {
			return srgses;
		}

		public void setSrgses(List<StoreRestockGoods> srgses) {
			this.srgses = srgses;
		}
	}
}
